# -*- coding: utf-8 -*-
# @Time : 2022/10/26 0:03
# @Author : renyumeng
# @Email : 2035328756@qq.com
# @File : app.py
# @Project : pythonFile
import uvicorn

from typing import *
from utils import *
from fastapi import FastAPI
from pydantic import BaseModel
from MyCNN import MyNet

net_zcl = torch.load(
    r"G:\JavaProject\AlgorithmInterface\src\main\resources\pythonFile\net(xin)53_yuchuli_200_b50_wendyx.pkl")

net_wdyx = torch.load(r"G:\JavaProject\AlgorithmInterface\src\main\resources\pythonFile\net_yuchuli_e200_b50.pkl")

app = FastAPI()


class LoadJsonVo(BaseModel):
    dataType: str
    data: List[dict]


@app.post("/api/get/oil/data")
async def read_root(jsonData: LoadJsonVo):
    dataType = jsonData.dataType
    lstss = []
    name = []
    # TODO test




    returnData = {
        "code": 200,
        "msg": "success",
        "data": []
    }
    data = []
    inputs, l = DataLoadFactory.loadDataOptions(jsonData.data, dataType)


    if l < 40000:
        returnData["code"] = 201
        returnData["msg"] = "输入数据不满七天"
        return returnData
    else:
        len_week = l // 40000
        zcl = []
        wdyx = []
        for i in range(0, len_week):
            inputss = [inputs[0][40000 * i:40000 * (i + 1)]]

            result = [inputss]
            result = np.array(result)
            x_data_zcl = torch.from_numpy(result).float()
            # x_data_wdyx = torch.from_numpy(result).float()
            output_zcl = net_zcl.test(x_data_zcl)  # 正常率

            output_wdyx = net_wdyx.test(x_data_zcl)  # 稳定运行时间

            zcl.append(output_zcl.item())
            wdyx.append(output_wdyx.item())

        dataDict = {"oilName": "", "correctRate": np.mean(zcl), "stableRunningTime": np.mean(wdyx)}
        data.append(dataDict)
        returnData["data"] = data
        return returnData


if __name__ == '__main__':
    uvicorn.run(app, port=8000)
